import { request, parseTime } from "../../utils/util";

let app = getApp()
Page({
  data: {
    top: app.globalData.rightBarInfo.top,
    height: app.globalData.rightBarInfo.height,
    list: [],
    page: 1,
    loadMore: true,
  },
  toActivityDetail (ev) {
    let { id } = ev.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/activityDetail/index?id=${id}`
    })
  },
  // 获取列表数据
  getList(page) {
    wx.showLoading({
      title: '加载中..',
      mask: true
    })
    request({
      url: '/api/web/activity/index',
      params: {
        page
      },
      success: ({ code, data }) => {
        wx.hideLoading()
        if (code === 200) {
          const currentData = data.data.map(item => {
            item.start_date_show = parseTime(item.start_date, '{m}月{d}日')
            item.end_date_show = parseTime(item.end_date, '{m}月{d}日')
            return item;
          })
          if (data.current_page === 1) {
            this.data.list = []
          }
          this.setData({
            list: [
              ...this.data.list,
              ...currentData
            ],
            page: data.current_page,
            loadMore: data.current_page !== data.last_page
          })
        }
      }
    })
  },
  onReachBottom () {
    if (!this.data.loadMore) {
      return false
    }
    this.getList(this.data.page + 1)
  },
  onLoad () {
    this.getList();
  },
})
